﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MapViewer
{
	/// <summary>
	/// Interaction logic for ServerNavWindow.xaml
	/// </summary>
	public partial class ServerNavWindow : Window
	{
		private static readonly System.Text.RegularExpressions.Regex mapFile = new System.Text.RegularExpressions.Regex(@"map_\d+\.dat$", System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);

		public ServerNavWindow()
		{
			InitializeComponent();
			if (Properties.Settings.Default.NavWindowWidth >= 0)
				this.Width = Properties.Settings.Default.NavWindowWidth;
			if (Properties.Settings.Default.NavWindowHeight >= 0)
				this.Height = Properties.Settings.Default.NavWindowHeight;
		}

		private String resultUrl = string.Empty;

		public String Url
		{
			get
			{
				return resultUrl;
			}
		}

		private void UseFolder_Click(object sender, RoutedEventArgs e)
		{
			resultUrl = browser.Source.AbsoluteUri;
			DialogResult = true;
			Close();
		}

		private void Navigate_Click(object sender, RoutedEventArgs e)
		{
			browser.Source = new Uri(address.Text);
			MapViewer.Properties.Settings.Default.LastUrl = address.Text;
			Properties.Settings.Default.Save();
			browser.Focus();
		}

		private void browser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
		{
			if (mapFile.IsMatch(e.Uri.AbsoluteUri))
			{
				resultUrl = e.Uri.AbsoluteUri;
				DialogResult = true;
				e.Cancel = true;
				Close();
			}
		}

		private void browser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
		{
			mshtml.IHTMLElementCollection links = (browser.Document as mshtml.HTMLDocument).getElementsByTagName("a");
			foreach (var link in links)
			{
				mshtml.IHTMLAnchorElement anchor = link as mshtml.IHTMLAnchorElement;
				if (anchor != null)
				{
					if (!string.IsNullOrEmpty(anchor.href))
					{
						if (mapFile.IsMatch(anchor.href))
						{
							useFolderButton.IsEnabled = true;
							return;
						}
					}
				}
			}
			useFolderButton.IsEnabled = false;
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			if (!string.IsNullOrEmpty(MapViewer.Properties.Settings.Default.LastUrl))
			{
				address.Text = MapViewer.Properties.Settings.Default.LastUrl;
				browser.Source = new Uri(address.Text);
				browser.Focus();
			}
		}

		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			Properties.Settings.Default.NavWindowWidth = (int)this.Width;
			Properties.Settings.Default.NavWindowHeight = (int)this.Height;
			Properties.Settings.Default.Save();
		}
	}
}
